﻿using DLLPC.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace VisionCore
{
    public class Status_SortNGST : ProcedureHelper_Exc
    {
        string subStatusCode = "";
        string tempStatusCode = "";
        bool condition = false;
        bool start = false;

        SystemTimeCounter scStatus = new SystemTimeCounter();
        public void Main()
        {
            condition =  Kernel.motion.ExcTR_X.IsHomeAlready && Kernel.motion.ExcTR_R.IsHomeAlready && Kernel.motion.ExcTR_Z.IsHomeAlready
                     && Kernel.motion.SortST_X.IsHomeAlready && Kernel.motion.SortST_Y.IsHomeAlready && Kernel.motion.SortST_R.IsHomeAlready;
            start = condition && AutoMode && !DryRunMode && false;

            switch (SortNGSTStatusCode)
            {
                case SortNGSTStatus.Init:
                    subStatusCode = "00: 准备";
                    {
                        if (start)
                        {
                            
                        }
                    }
                    break;
                case SortNGSTStatus.XY_ToWait:
                    subStatusCode = "01: XY移动到安全位";
                    {
                        if (SortST_X_Wait && SortST_Y_Wait)
                        {
                            if (!ExcTR_FlipReq)
                            {
                                SortNGSTStatusCode = SortNGSTStatus.CheckStatus;
                            }
                            else
                            {
                                SortNGSTStatusCode = SortNGSTStatus.XY_ToUnloadforTRFlip;
                            }
                        }
                    }
                    break;
                case SortNGSTStatus.XY_ToUnloadforTRFlip:
                    subStatusCode = "02: XY移动到载出位避让for倒膜移载翻转";
                    {
                        if (SortST_X_ULD && SortST_Y_ULD && !ExcTR_FlipReq)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.CheckStatus;
                        }
                    }
                    break;
                case SortNGSTStatus.CheckStatus:
                    subStatusCode = "05: 状态判断";
                    {
                        if (true)
                        {
                            //if (ParameterHelper.SortSTNG.SortCompletFlag)
                            //{
                            //    SortNGSTStatusCode = SortNGSTStatus.Init;
                            //}
                            //else
                            //{
                            //    if(!LaserIDAutoGetPass && !ParameterHelper.SortSTNG.laserIniInfo.IniFileReadFlag)
                            //    {
                            //        SortNGSTStatusCode = SortNGSTStatus.IniFileRead;//INI文件读取
                            //    }
                            //    else if (!SortNGSTFirstAlignOK &&VisionAutoMode)
                            //    {
                            //        RTCHelper.DIO.L_SortNGSTWaferID = ParameterHelper.SortSTNG.Wafer_ID;
                            //        SortNGSTStatusCode = SortNGSTStatus.PreAlignReq;//初对位请求
                            //    }
                            //    else if (SortNGSTFirstAlignOK)
                            //    {
                            //        RTCHelper.DIO.L_SortNGSTWaferID = ParameterHelper.SortSTNG.Wafer_ID;
                            //        SortNGSTStatusCode = SortNGSTStatus.Sorter;//挑拣
                            //    }
                            //}
                        }
                      
                    }
                    break;
                # region Load 上料
                case SortNGSTStatus.LoadReq: //Load
                    subStatusCode = "10: 入料请求";
                    
                    break;
                case SortNGSTStatus.XY_ToWait_LD:
                    subStatusCode = "11: XY移动到安全位";
                    {
                        if (SortST_X_Wait && SortST_Y_Wait)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.XR_ToLoad_ULD;
                        }
                    }
                    break;
                case SortNGSTStatus.XR_ToLoad_LD:
                    subStatusCode = "12: XR移动到NG入料位";
                    {
                        if (SortST_X_LD)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.Y_ToLoad_LD;
                        }

                    }
                    break;
                case SortNGSTStatus.Y_ToLoad_LD:
                    subStatusCode = "13: Y移动到NG入料位";
                    {
                        if (SortST_Y_LD)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.NGCylinder_ToUp_LD;
                        }
                    }
                    break;
                case SortNGSTStatus.NGCylinder_ToUp_LD:
                    subStatusCode = "14: NG平台气缸上升";
                    {
                        if (SortST_NG_Up)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.DoorOpen_LD;
                        }
                    }
                    break;
                case SortNGSTStatus.DoorOpen_LD:
                    subStatusCode = "15: 安全门2解锁";
                    {
                        if (Kernel.Exc.DI.X0E5_Door2==0)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.NGST_VacOn;
                        }
                    }
                    break;
                case SortNGSTStatus.NGST_VacOn:
                    subStatusCode = "20: NG平台真空ON";
                    {
                        if (SortST_NG_Vac)
                        {
                            ParameterHelper.SortSTNG = new ChipInfo();
                            SaveSortSTNG();
                            SortNGSTStatusCode = SortNGSTStatus.NGST_VacOn;
                        }
                    }
                    break;
                case SortNGSTStatus.WaitReadLotNo:
                    subStatusCode = "21: 等待输入LotNo";
                    {
                        if (SortST_NG_Vac && ParameterHelper.SortSTNG.Wafer_ID != "" && ParameterHelper.SortSTNG.Wafer_ID != null)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.PresenceUpdata_LD;
                        }
                    }
                    break;
                case SortNGSTStatus.PresenceUpdata_LD:
                    subStatusCode = "22: 资料更新";
                    {
                        ParameterHelper.SortSTNG.Enable = true;
                        SaveSortSTNG();
                      
                    }
                    break;
                case SortNGSTStatus.DoorClose_LD:
                    subStatusCode = "15: 安全门2关闭";
                    {
                        if (Kernel.Exc.DI.X0E5_Door2 == 1 && Kernel.Exc.DO.Y1E5_DoorLock2==1)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.LoadComplete;
                        }
                    }
                    break;
                case SortNGSTStatus.LoadComplete:
                    subStatusCode = "23: 入料完成";
                    {
                        SortNGSTStatusCode = SortNGSTStatus.Init;
                    }
                    break;
                #endregion

                #region InI文件读取
                case SortNGSTStatus.IniFileRead://INI文件读取
                    subStatusCode = "40: InI文件读取";

                    //FilesINI filesINI = new FilesINI();
                    //if (!LaserIDAutoGetPass)//20230202liang新增
                    //{
                    //    laserIniPath = "C:\\Download" + "\\" + ParameterHelper.SortSTNG.Wafer_ID + ".ini";
                    //    if (File.Exists(laserIniPath))//20230202liang新增
                    //    {
                    //        ParameterHelper.SortSTNG.laserIniInfo.IniContent.X = Convert.ToDouble(filesINI.INIRead("Content", "X", laserIniPath));
                    //        ParameterHelper.SortSTNG.laserIniInfo.IniContent.Y = Convert.ToDouble(filesINI.INIRead("Content", "Y", laserIniPath));
                    //        ParameterHelper.SortSTNG.laserIniInfo.IniContent.Line1 = filesINI.INIRead("Content", "LINE1", laserIniPath);
                    //        ParameterHelper.SortSTNG.laserIniInfo.IniContent.Line2 = filesINI.INIRead("Content", "LINE2", laserIniPath);
                    //        ParameterHelper.SortSTNG.laserIniInfo.IniContent.Line3 = filesINI.INIRead("Content", "LINE3", laserIniPath);
                    //        ParameterHelper.SortSTNG.laserIniInfo.IniContent.Line4 = filesINI.INIRead("Content", "LINE4", laserIniPath);
                    //    }
                    //    if (!LaserIDAutoGetPass && File.Exists(laserIniPath) && ParameterHelper.SortSTNG.laserIniInfo.IniContent.Line1.Length <= ParameterHelper.CurParam.LaserParam.LaserBlockColNum && ParameterHelper.SortSTNG.laserIniInfo.IniContent.Line2.Length <= ParameterHelper.CurParam.LaserParam.LaserBlockColNum && ParameterHelper.SortSTNG.laserIniInfo.IniContent.Line3.Length <= ParameterHelper.CurParam.LaserParam.LaserBlockColNum && ParameterHelper.SortSTNG.laserIniInfo.IniContent.Line4.Length <= ParameterHelper.CurParam.LaserParam.LaserBlockColNum && (ParameterHelper.SortSTNG.laserIniInfo.IniContent.Line1.Length >0|| ParameterHelper.SortSTNG.laserIniInfo.IniContent.Line2.Length > 0|| ParameterHelper.SortSTNG.laserIniInfo.IniContent.Line3.Length > 0||ParameterHelper.SortSTNG.laserIniInfo.IniContent.Line4.Length > 0))//20230202liang新增
                    //    {
                    //        ParameterHelper.SortSTNG.laserIniInfo.IniFileReadFlag = true;
                    //        SaveSortST();
                    //        SortNGSTStatusCode = SortNGSTStatus.CheckStatus;
                    //    }
                    //}

                    break;
                #endregion

                #region 初对位
                case SortNGSTStatus.PreAlignReq:
                    subStatusCode = "60: 初对位请求";
                    {
                        //RTCHelper.IOEvent.HSG_SortSTFirstAlignmentTrigger.Trig();
                        SortNGSTFirstAlignOK = false;
                        //ParameterHelper.SortSTNG.ReadMapFlag = false;
                        SaveSortSTNG();
                        SortNGSTStatusCode = SortNGSTStatus.PreAlignComplete;                       

                    }
                    break;
                case SortNGSTStatus.PreAlignComplete:
                    subStatusCode = "61: 初对位完成";
                    {
                        if (SortNGSTFirstAlignOK)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.CheckStatus;
                        }
                    }
                    break;
                #endregion

                #region 挑拣
                case SortNGSTStatus.Sorter://进入挑选
                    subStatusCode = "70: 进入挑选";
                    {
                        //if (SortStatusCode == SortStatus.Init && ParameterHelper.SortSTNG.SortCompletFlag)
                        //{
                        //    SortNGSTStatusCode = SortNGSTStatus.SorterComplete;
                        //}
                        //else if(SortStatusCode == SortStatus.Init && ExcTR_FlipReq)
                        //{
                        //    SortNGSTStatusCode = SortNGSTStatus.XY_ToWait;
                        //}
                    }
                    break;
                case SortNGSTStatus.SorterComplete:
                    subStatusCode = "71: 挑选完成";
                    {
                        //if (ParameterHelper.SortSTNG.SortCompletFlag)
                        //{
                        //    SortSTStatusCode = SortSTStatus.XY_ToWait;
                        //}
                    }
                    break;
                #endregion

                #region Unload 下料
                case SortNGSTStatus.UnloadReq:
                    subStatusCode = "80: 出料请求";
                    {
                        SortNGSTStatusCode = SortNGSTStatus.XY_ToWait_ULD;

                    }
                    break;
                case SortNGSTStatus.XY_ToWait_ULD:
                    subStatusCode = "81: XY移动到安全位";
                    {
                        if (SortST_X_Wait && SortST_Y_Wait)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.XR_ToLoad_ULD;
                        }
                    }
                    break;
                case SortNGSTStatus.XR_ToLoad_ULD:
                    subStatusCode = "82: XR移动到NG出料位";
                    {
                        if (SortST_X_LD)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.Y_ToLoad_ULD;
                        }
                    }
                    break;
                case SortNGSTStatus.Y_ToLoad_ULD:
                    subStatusCode = "83: Y移动到NG出料位";
                    {
                        if (SortST_Y_LD)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.NGCylinder_ToUp_ULD;
                        }
                    }
                    break;
                case SortNGSTStatus.NGCylinder_ToUp_ULD:
                    subStatusCode = "84: NG平台气缸上升";
                    {
                        if (SortST_NG_Up)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.DoorOpen_LD;
                        }
                    }
                    break;
                case SortNGSTStatus.DoorOpen_ULD:
                    subStatusCode = "85: 安全门2解锁";
                    {
                        if (Kernel.Exc.DI.X0E5_Door2 == 0 && Kernel.Exc.DI.X0F0_SortST_NGLDReq==1)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.NGST_VacOff;
                        }
                    }
                    break;
                case SortNGSTStatus.NGST_VacOff:
                    subStatusCode = "90: NG平台真空ON";
                    {
                        if (!SortST_NG_Vac && Kernel.Exc.DI.X0F1_SortST_NGLDComp == 1)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.PresenceUpdata_ULD;
                        }
                    }
                    break;
                case SortNGSTStatus.PresenceUpdata_ULD:
                    subStatusCode = "91: 资料更新";
                    {
                        ParameterHelper.SortSTNG = new ChipInfo();
                        SaveSortSTNG(); 
                        
                    }
                    break;
                case SortNGSTStatus.DoorClose_ULD:
                    subStatusCode = "92: 安全门2关闭";
                    {
                        if (Kernel.Exc.DI.X0E5_Door2 == 1 && Kernel.Exc.DO.Y1E5_DoorLock2 == 1&& Kernel.Exc.DI.X0F1_SortST_NGLDComp == 1)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.UnLoadComplete;
                        }
                        else if(Kernel.Exc.DI.X0F0_SortST_NGLDReq == 1)
                        {
                            SortNGSTStatusCode = SortNGSTStatus.NGST_VacOn;
                        }
                    }
                    break;
                case SortNGSTStatus.UnLoadComplete:
                    subStatusCode = "93: 出料完成";
                    {
                        SortNGSTStatusCode = SortNGSTStatus.Init;
                    }
                    break;
                #endregion

                default:
                    break;
            }

            LogStatusCode(subStatusCode);
        }

        private void LogStatusCode(string msg)
        {
            if (msg != tempStatusCode)
            {
                tempStatusCode = msg;
                LogMsg("挑拣NG平台- " + tempStatusCode);
            }
        }
    }
}
